package com.dev.dream.exochatlockscreen.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dev.dream.exochatlockscreen.EXOChatKeypadPreferences;
import com.dev.dream.exochatlockscreen.LockScreenAppActivity;
import com.dev.dream.exochatlockscreen.R;
import com.dev.dream.exochatlockscreen.model.AppToolbarItem;

public class AppToolbarAdapter extends BaseAdapter {

	LockScreenAppActivity lockActivity;
	private Context context;
	private ArrayList<AppToolbarItem> appToolbar;
	//private final BitmapCache mMemoryCache;

	public AppToolbarAdapter(Context context,
			ArrayList<AppToolbarItem> appToolbar,
			LockScreenAppActivity lockActivity) {
		this.context = context;
		this.appToolbar = appToolbar;
		//this.mMemoryCache = new BitmapCache();
		this.lockActivity = lockActivity;
	}

	@Override
	public int getCount() {
		return appToolbar.size();
	}

	@Override
	public Object getItem(int position) {
		return appToolbar.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHolder {
		ImageView imgIcon;
		TextView txtTitle;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) context
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.item_toolbar, null);

			holder = new ViewHolder();

			holder.imgIcon = (ImageView) convertView
					.findViewById(R.id.img_icon_app_toolbar);
			holder.txtTitle = (TextView) convertView
					.findViewById(R.id.tv_info_input_passcode);

			convertView.setTag(holder);

			holder.imgIcon.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ImageView img = (ImageView) v;
					AppToolbarItem info = (AppToolbarItem) img.getTag();

					if (info.getId() != 4) {
						if (EXOChatKeypadPreferences.getInstance()
								.isEnablePasscode()) {
							lockActivity.setType_finish(info.getId() + 1);
							lockActivity.sendMessageFromToolbar();
						} else {
							lockActivity.startFromToolbar(info.getId() + 1);
						}
					} else {

						if (EXOChatKeypadPreferences.getInstance().isFlashlight()) {

							EXOChatKeypadPreferences.getInstance().setFlashlight(
									false);
						} else {

							EXOChatKeypadPreferences.getInstance().setFlashlight(
									true);

						}
						notifyDataSetChanged();
						lockActivity.setFlashlight();

					}

				}
			});
		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		// convertView.setClickable(true);

		// imgIcon.setImageResource(appAdsItem.get(position).getIcon());
		AppToolbarItem info = appToolbar.get(position);
		holder.txtTitle.setText(info.getApp_name());
		// holder.txtTitle.setSelected(true);

		// Log.d("Kai", appToolbar.get(position).getIcon() +"");

		// System.out.println(appAdsItem.get(position).getRate() +"");

		setImageView(holder, position);
		holder.imgIcon.setTag(info);
		if (position == 4 && EXOChatKeypadPreferences.getInstance().isFlashlight()) {
			holder.imgIcon.setSelected(true);
		} else {
			holder.imgIcon.setSelected(false);
		}

		return convertView;
	}

	private void setImageView(final ViewHolder viewHolder, final int position) {

		int imageResId = appToolbar.get(position).getIcon();
		viewHolder.imgIcon.setImageResource(imageResId);

		// Log.d("Kai", imageResId +"");
		//
		// Bitmap bitmap = getBitmapFromMemCache(imageResId);
		// if (bitmap == null) {
		// bitmap = BitmapFactory.decodeResource(this.context.getResources(),
		// imageResId);
		// addBitmapToMemoryCache(imageResId, bitmap);
		// }
		// viewHolder.imgIcon.setImageBitmap(bitmap);
	}

//	private void addBitmapToMemoryCache(final int key, final Bitmap bitmap) {
//		if (getBitmapFromMemCache(key) == null) {
//			mMemoryCache.put(key, bitmap);
//		}
//	}
//
//	private Bitmap getBitmapFromMemCache(final int key) {
//		return mMemoryCache.get(key);
//	}

}
